package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.StudentInfor;
import com.ruoyi.system.service.IStudentInforService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/system/infor")
public class StudentInforController extends BaseController
{
    @Autowired
    private IStudentInforService studentInforService;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:infor:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentInfor studentInfor)
    {
        startPage();
        List<StudentInfor> list = studentInforService.newSelectStudentInforList(studentInfor);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:infor:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody StudentInfor studentInfor)
    {
        List<StudentInfor> list = studentInforService.newSelectStudentInforList(studentInfor);
        ExcelUtil<StudentInfor> util = new ExcelUtil<StudentInfor>(StudentInfor.class);
        util.exportExcel(response, list, "学生基本信息");
    }
    /**
     * 导入【请填写功能名称】列表
     */
    @PostMapping("/importData")
    public void importData(HttpServletResponse response,MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<StudentInfor> util = new ExcelUtil<StudentInfor>(StudentInfor.class);
        List<StudentInfor> userList = util.importExcel(file.getInputStream());
        List<StudentInfor> resultList=studentInforService.importData(userList);
        util.exportExcel(response, resultList, "问题数据");
//        if(resultList!=null&&resultList.size()!=0){
//            util.exportExcel(response, resultList, "问题数据");
//        }
    }

    //补充导入数据
    @PostMapping("/addImportData")
    public void addImportData(HttpServletResponse response,MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<StudentInfor> util = new ExcelUtil<StudentInfor>(StudentInfor.class);
        List<StudentInfor> userList = util.importExcel(file.getInputStream());
        List<StudentInfor> resultList=studentInforService.addImportData(userList);
        util.exportExcel(response, resultList, "问题数据");
//        if(resultList!=null&&resultList.size()!=0){
//            util.exportExcel(response, resultList, "问题数据");
//        }
    }


    /**
     * 根据身份证号查询
     */
//    @PreAuthorize("@ss.hasPermi('system:infor:query')")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(String studentIdCard)
    {
        return success(studentInforService.selectStudentInforByStudentIdCard(studentIdCard));
    }
    @Anonymous
    //开学时输入身份证查询信息
    @GetMapping("/getSchoolOpensInfoByIdCard")
    public AjaxResult getSchoolOpensInfoByIdCard(String studentIdCard)
    {
        return success(studentInforService.getSchoolOpensInfoByIdCard(studentIdCard));
    }

    @Anonymous
    //开学时学生更新状态
    @GetMapping("/updateStudentStatusByIdCard")
    public AjaxResult updateStudentStatusByIdCard(StudentInfor studentInfor)
    {
        return success(studentInforService.updateStudentStatusByIdCard(studentInfor));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:infor:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody StudentInfor studentInfor)
    {
        if(studentInfor.getStudentIdCard()==null||"".equals(studentInfor.getStudentIdCard())){
            return error("身份证号不能为空");
        }
        return toAjax(studentInforService.insertStudentInfor(studentInfor));
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:infor:edit')")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody StudentInfor studentInfor)
    {
        if(studentInfor.getStudentIdCard()==null||"".equals(studentInfor.getStudentIdCard())){
            return error("身份证号不能为空");
        }
        return toAjax(studentInforService.updateStudentInfor(studentInfor));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:infor:remove')")
	@DeleteMapping("/{studentIdCards}")
    public AjaxResult remove(@PathVariable String[] studentIdCards)
    {
        return toAjax(studentInforService.deleteStudentInforByStudentIdCards(studentIdCards));
    }

    @PostMapping("/countStudent")
    public List<Map<String,Object>> countStudent(@RequestBody StudentInfor studentInfor)
    {
        return studentInforService.countStudent(studentInfor);
    }
}
